package org.helgoboss.basic_one_filer_template

import org.helgoboss.project_data._
import java.io.File
import org.helgoboss.one_filer_template.OneFilerTemplate
import org.helgoboss.scala_additions.Implicits._
import java.net.URL

object BasicOneFilerTemplate extends OneFilerTemplate {
    val id = "org.helgoboss.basic"

    def extractProjectData(projectMap: Map[String, Any], sourceCodeParam: String, sourceFile: File) = {
        lazy val mapBasedGroupId = projectMap.get("groupId").map(_.toString)
        lazy val mapBasedArtifactId = projectMap.get("artifactId").map(_.toString)
        lazy val (fileBasedGroupId, fileBasedArtifactId) = {
            val FileNameRegex = """(.*)\.(.*)\.scala""".r
            sourceFile.getName match {
                case FileNameRegex(groupId, artifactId) => (Some(groupId), Some(artifactId))
                case _ => (None, None)
            }
        }
        
        val givenObrRepositories: Set[URL] = projectMap.getDeep("deployment", "obrRepositories") match {
            case Some(l: List[String]) => 
                l.map(new URL(_)).toSet
            case None => 
                Set.empty
        }
        
        val givenObrResources: Set[ObrResourceId] = projectMap.getDeep("deployment", "obrResources") match {
            case Some(l: List[String]) =>
                l.map(generateObrResourceId).toSet
            case None =>
                Set.empty
        }

        val tmpSourceFile = sourceFile
        
        new ProjectData {
            val sourceCode = sourceCodeParam
            val projectId = new ProjectId {
                val groupId = (mapBasedGroupId orElse fileBasedGroupId) getOrElse error("could not determine groupId")
                val artifactId = (mapBasedArtifactId orElse fileBasedArtifactId) getOrElse error("could not determine artifactId")
                val version = projectMap("version").toString
            }
            val sourceFile = Some(tmpSourceFile)
            val name = projectMap.get("name").map(_.toString)
            val description = projectMap.get("description").map(_.toString)
            val dependencies = {
                val dependencySet = projectMap("dependencies").asInstanceOf[List[String]].toSet
                dependencySet.map(dependencyIdExpression => generateDependencyId(dependencyIdExpression))
            }
            val bundleActivator = projectMap.get("bundleActivator").map(_.toString)
            val parentProjectId = None
            val commandletId = projectMap.get("commandletId").map(_.toString)
            val obrRepositories = givenObrRepositories
            val obrResources = givenObrResources
        }
    }
        
    private def generateDependencyId(idExpression: String) = {
        val expressionParts = idExpression.split(":")
        new DependencyId {
            val groupId = expressionParts(0)
            val artifactId = expressionParts(1) 
            val version = expressionParts(2)
            val `type` = expressionParts.lift(3) getOrElse "jar"
        }
    }
    
    private def generateObrResourceId(idExpression: String) = {
        val expressionParts = idExpression.split(":")
        new ObrResourceId {
            val symbolicName = expressionParts(0)
            val version = expressionParts(1)
        }
    }
}